'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('ai_recommendations', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      user_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'users',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      type: {
        type: Sequelize.ENUM('outfit', 'occasion'),
        allowNull: false,
        comment: '推荐类型：outfit-搭配推荐, occasion-场合推荐'
      },
      occasion: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '场合类型'
      },
      weather_info: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '天气信息'
      },
      clothing_ids: {
        type: Sequelize.JSON,
        allowNull: false,
        comment: '推荐的衣服ID列表'
      },
      ai_provider: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: 'AI提供商：volcano, doubao, deepseek等'
      },
      ai_analysis: {
        type: Sequelize.TEXT('long'),
        allowNull: true,
        comment: 'AI原始分析结果'
      },
      recommendation_data: {
        type: Sequelize.JSON,
        allowNull: false,
        comment: '完整的推荐数据'
      },
      style_tags: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '风格标签'
      },
      reason: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '推荐理由'
      },
      score: {
        type: Sequelize.DECIMAL(3, 2),
        allowNull: false,
        defaultValue: 0.8,
        comment: '推荐评分'
      },
      user_rating: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '用户评分 1-5'
      },
      user_feedback: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '用户反馈'
      },
      is_favorite: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false,
        comment: '是否收藏'
      },
      usage_count: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '使用次数'
      },
      last_used_at: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: '最后使用时间'
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    // 添加索引
    await queryInterface.addIndex('ai_recommendations', ['user_id']);
    await queryInterface.addIndex('ai_recommendations', ['type']);
    await queryInterface.addIndex('ai_recommendations', ['occasion']);
    await queryInterface.addIndex('ai_recommendations', ['user_id', 'type']);
    await queryInterface.addIndex('ai_recommendations', ['created_at']);
    await queryInterface.addIndex('ai_recommendations', ['score']);
    await queryInterface.addIndex('ai_recommendations', ['is_favorite']);
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('ai_recommendations');
  }
};
